package cn.edu.hitsz.infra;

import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import populus.core.queue.UrlItem;

/**
 *  Extract links from frames
 * @author mingchen
 * @date 
 *
 */
public class FrameLinkExtract implements LinkExtract {

	private final String content;
	private final UrlItem reference;
	
	/**
	 *  
	 * @param content 
	 * 				the content of the response
	 * @param url  
	 * 				the reference url item
	 */
	public FrameLinkExtract(String content,UrlItem url){
		if (content.equals(null) || content.trim().equals("")) {
			throw new IllegalArgumentException("The content cannont be null");
		}
		if(url.equals(null)||url.getUrl().trim().equals("")){
			try {
				throw new MalformedURLException("The URL cannot be null");
			} catch (MalformedURLException e) {
				e.printStackTrace();
			}
		}
		this.content = content;
		this.reference = url;
	}
	
	/**
	 * Extract full urls from frame links
	 */
	public List<String> extractLinks(){
		List<String> frameLinks = new ArrayList<String>();
		Pattern pattern = Pattern.compile("(?i)(?s)<\\s*?frame.*?src=\"(.*?)\".*?>");
		Matcher matcher = pattern.matcher(this.content);
		while(matcher.find()){
			String url = matcher.group(1);
			// If the url path is relative path
			if(url.startsWith("/")){
				if(this.reference.getUrl().trim().endsWith("/")){
					url = this.reference.getUrl().trim() + url;
				}
			}
			frameLinks.add(url);
		}
		return frameLinks;
	}
	
	/**
	 * 
	 *  Packge all the urls into UrlItems
	 * @param urls
	 * @return
	 * List<UrlItem>
	 * 
	 */
	public List<UrlItem> packgeToUrlItem(List<String> urls){
		List<UrlItem> urlitems = new ArrayList<UrlItem>();
		for(String url:urls){
			urlitems.add(new UrlItem(url,this.reference.getDepth()+1));
		}
		return urlitems;
	}
	
	/**
	 * 
	 * extract the links and then package them to UrlItems
	 * @return
	 * List<UrlItem>
	 * 2015年8月17日下午4:31:34
	 */
	public List<UrlItem> offerLinks(){
		return packgeToUrlItem(extractLinks());
	}
}
